﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CookieValueCollection.cs" company="Radical Research Ltd">
//   Copyright (c) Radical Research Ltd. The MIT License (MIT)
// </copyright>
// <summary>
//   
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RadicalResearch.MiniState.Cookies
{
    using System;
    using System.Collections;
    using System.Web;

    using RadicalResearch.MiniState;

    /// <summary>
    /// Exposes an HTTP cookie as an IValueCollection
    /// </summary>
    public class CookieValueCollection : IValueCollection
    {
        #region Constants and Fields

        /// <summary>
        /// Http cookie
        /// </summary>
        private readonly HttpCookie cookie;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CookieValueCollection"/> class.
        /// </summary>
        /// <param name="cookie">The cookie.</param>
        public CookieValueCollection(HttpCookie cookie)
        {
            if (null == cookie)
            {
                throw new ArgumentNullException("cookie");
            }

            this.cookie = cookie;
        }

        #endregion

        #region Properties

        /// <summary>Gets the http cookie.</summary>
        public HttpCookie Cookie
        {
            get
            {
                return this.cookie;
            }
        }

        #endregion

        #region Indexers

        /// <summary>
        /// Gets or sets the entry with the specified key
        /// </summary>
        /// <param name="key">The String key of the entry to locate.</param>
        /// <returns>String value for specified key.</returns>
        public string this[string key]
        {
            get
            {
                return this.cookie.Values[key];
            }

            set
            {
                this.cookie.Values[key] = value;
            }
        }

        /// <summary>
        /// Removes the values with the specified key from the collection.
        /// </summary>
        /// <param name="key">The String key of the value to remove.</param>
        public void Remove(string key)
        {
            this.cookie.Values.Remove(key);
        }

        #endregion

        #region Public Methods

        /// <summary>Sets the cookie on the current HTTP response.</summary>
        public void Set()
        {
            HttpContext.Current.Response.Cookies.Set(this.cookie);
        }

        #endregion

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator GetEnumerator()
        {
            return this.cookie.Values.GetEnumerator();
        }
    }
}